package com.tupu.controller.dispatch;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tupu.common.PageResult;
import com.tupu.common.Result;
import com.tupu.entity.dispatch.*;
import com.tupu.entity.dto.StandardizationPointDTO;
import com.tupu.service.dispatch.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

/**
 * 一体化信号
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/integration")
public class IntegratedSignalController {
    @Autowired
    private StandardizationPointService standardizationPointService;

    @Autowired
    private SignalmissingDataService signalmissingDataService;
    @Autowired
    private SignalfrequencyDataService signalfrequencyDataService;

    @Autowired
    private SignalfrequencyLevelService signalfrequencyLevelService;

    @Autowired(required = false)
    private StandardizationDeviceNewsService standardizationDeviceNewsService;

    @Autowired(required = false)
    private StandardizationWarningSignalService standardizationWarningSignalService;

    @Autowired(required = false)
    private StandardizationControlMsgService standardizationControlMsgService;

    @Autowired(required = false)
    private StandardizationOperDataService standardizationOperDataService;

    @Autowired(required = false)
    private SignalOmissionPointTableMsgService signalOmissionPointTableMsgService;

    @Autowired(required = false)
    private SignalOmissionRealtimeDataService signalOmissionRealtimeDataService;

    @Autowired(required = false)
    private SignalOmissionHistoryDataService signalOmissionHistoryDataService;

    /**
     * 标准化-设备参数-设备类型列表
     *
     * @return
     */
    @PostMapping("/standardizationDeviceNewsDeviceTypeList")
    public Result standardizationDeviceNewsDeviceTypeList() {
        List<String> list = standardizationDeviceNewsService.findAllDeviceType();
        return Result.ok(list);
    }

    /**
     * 标准化-告警信号 列表、分页
     *
     * @param page
     * @param size
     * @param signal_type
     * @param device
     * @param problem_type
     * @return
     */
    @PostMapping("/standardizationWarningSignalList")
    public Result standardizationWarningSignalList(@RequestParam(required = false, defaultValue = "1") Long page,
                                                   @RequestParam(required = false, defaultValue = "10") Long size,
                                                   //信号类型
                                                   @RequestParam(required = false) String signal_type,
                                                   //设备
                                                   @RequestParam(required = false) String device,
                                                   //问题类型
                                                   @RequestParam(required = false) String problem_type) {
        try {
            Page<StandardizationWarningSignal> pageInfo = standardizationWarningSignalService.warningSignalPage(signal_type, device, problem_type, page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.ok(new Page<>());
        }
    }

    /**
     * 标准化-告警信号 列表、分页
     *
     * @param page
     * @param size
     * @param station
     * @param pointName
     * @param checkResult
     * @return
     */
    @PostMapping("/standardizationDeviceNewsList")
    public Result standardizationSignalList(@RequestParam(required = false, defaultValue = "1") Long page,
                                            @RequestParam(required = false, defaultValue = "10") Long size,
                                            //厂站
                                            @RequestParam(required = false) String station,
                                            //点名
                                            @RequestParam(required = false) String pointName,
                                            //校验结果
                                            @RequestParam(required = false) String checkResult) {
        try {
            Page<StandardizationPoint> pageInfo = standardizationPointService.pointSignalPage(station, pointName, checkResult, page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            log.info(e.toString());
            return Result.ok(new Page<>());
        }
    }

    /**
     * 编辑 校验结果
     *
     * @return
     */
    @PostMapping("/updateValidResult")
    public Result updateValidResult(@RequestParam(required = false) Integer problemId,
                                    @RequestParam(required = false) String validResult) {
        try {
            standardizationPointService.updateValidResult(problemId, validResult);
            return Result.ok("编辑成功");
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }

    /**
     * 标准化-定时分析 T0007点表，分析厂站和点名
     *
     * @return
     */
    @PostMapping("/analysisT0007")
    public Result analysisT0007(@RequestParam(required = false) Integer C0007_POINT_ID) {
        try {
            standardizationPointService.analysisT0007(C0007_POINT_ID);
            return Result.ok("分析T0007点表，分析厂站和点名成功");
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }
 /*   @PostMapping("/validPointData")
    public Result validPointData(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            standardizationPointService.validPointData(startTime, endTime);
            return Result.ok();
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }*/

    /**
     * 标准化-信号 下载模板
     *
     * @return
     */
    @GetMapping("/standardizationDeviceTemplate")
    public void standardizationDeviceTemplate(HttpServletResponse httpResponse) {
        try {
            standardizationPointService.downloadExcelDemo(httpResponse);
        } catch (Exception e) {
            log.error(e.toString());
        }
    }

    /**
     * 标准化-信号 Excel导入
     *
     * @return
     */
    @PostMapping("/standardizationAnalysisImport")
    public Result standardizationDeviceImport(MultipartFile file, HttpServletResponse response) {
        try {
            //Map<String, Object> resultMap = new HashMap<>();
            //resultMap.put("fileId", standardizationPointService.standardizationDeviceImport(file, response));
            Map<String, Object> resultMap = standardizationPointService.standardizationDeviceImport(file, response);
            return Result.ok(resultMap);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 标准化-信号 Excel导入 结果下载
     *
     * @return
     */
    @RequestMapping("/downloadCheckResult")
    public void downloadCheckResult(@RequestParam String fileId, HttpServletResponse response) {
        try {
            standardizationPointService.downloadCheckResult(fileId, response);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    /**
     * 标准化-信号 Excel导出
     *
     * @return
     */
    @RequestMapping("/standardizationExportExcel")
    public void standardizationExportExcel(HttpServletResponse response,
                                           //厂站
                                           @RequestParam(required = false) String station,
                                           //设备类型
                                           @RequestParam(required = false) String messageType,
                                           //校验结果
                                           @RequestParam(required = false) String checkResult,
                                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            standardizationPointService.exportExcel(response, station, messageType, checkResult, startTime, endTime);
        } catch (Exception e) {
            log.error(e.toString());
        }
    }

    @PostMapping("/checkFrequency")
    public Result checkFrequency(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            signalfrequencyDataService.countFrequency(startTime, endTime);
            return Result.ok();
        } catch (Exception e) {
            log.error(e.toString());
            return Result.error(e.toString());
        }
    }

    @PostMapping("/checkMissingSignal")
    public Result checkMissingSignal(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                     @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            signalmissingDataService.findMissingSignal(startTime, endTime);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    /**
     * 不合格类型占比
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @PostMapping("/standardizationDeviceNewsNotQualifiedDevicePercent")
    public Result<?> pointTypePercentage(//厂站
                                         @RequestParam(required = false) String station,
                                         //设备类型
                                         @RequestParam(required = false) String messageType,
                                         //校验结果
                                         @RequestParam(required = false) String checkResult,
                                         @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                         @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            return Result.ok(standardizationPointService.pointTypePercentage(station, messageType, checkResult, startTime, endTime));
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }

    /**
     * 合格不合格占比
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @PostMapping("/standardizationDeviceNewsQualifiedPercentage")
    public Result resultPercentage(
            //厂站
            @RequestParam(required = false) String station,
            //设备类型
            @RequestParam(required = false) String messageType,
            //校验结果
            @RequestParam(required = false) String checkResult,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            return Result.ok(standardizationPointService.resultPercentage(station, messageType, checkResult, startTime, endTime));
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }

    /**
     * 标准化-设备类型列表
     *
     * @return
     */
    @PostMapping("/standardizationDeviceTypeList")
    public Result standardizationDeviceTypeList() {
        List<String> list = standardizationPointService.standardizationDeviceTypeList();
        return Result.ok(list);
    }

    /**
     * 标准化-厂站列表
     *
     * @return
     */
    @PostMapping("/standardizationDeviceNewsStationList")
    public Result standardizationStationList(@RequestParam(required = false) String station) {
        List<String> list = standardizationPointService.standardizationStationList(station);
        return Result.ok(list);
    }

    /**
     * 标准化-告警信号-设备类型列表
     *
     * @return
     */
    @PostMapping("/standardizationWarningSignalDeviceList")
    public Result standardizationWarningSignalDeviceList() {
        List<String> list = standardizationWarningSignalService.findAllDevice();
        return Result.ok(list);
    }

    /**
     * 标准化-告警信号-信号类型
     *
     * @return
     */
    @PostMapping("/standardizationWarningSignalSignalType")
    public Result standardizationWarningSignalSignalType() {
        List<String> list = standardizationWarningSignalService.findAllSignalType();
        return Result.ok(list);
    }

    /**
     * 标准化-告警信号-问题类型列表
     *
     * @return
     */
    @PostMapping("/standardizationWarningSignalProblemTypeList")
    public Result standardizationWarningSignalProblemTypeList() {
        List<String> list = standardizationWarningSignalService.findAllProblemType();
        return Result.ok(list);
    }

    /**
     * 标准化-告警信号-设备 百分比
     *
     * @return
     */
    @PostMapping("/standardizationWarningSignalDevicePercentage")
    public Result<Map<String, Object>> standardizationWarningSignalDevicePercentage() {
        Map<String, Object> result = standardizationWarningSignalService.devicePercentage();
        return Result.ok(result);
    }

    /**
     * 标准化-告警信号-问题类型 百分比
     *
     * @return
     */
    @PostMapping("/standardizationWarningSignalProblemTypePercentage")
    public Result<Map<String, Object>> standardizationWarningSignalProblemTypePercentage() {
        Map<String, Object> result = standardizationWarningSignalService.problemTypePercentage();
        return Result.ok(result);
    }

    /**
     * 标准化-告警信号-信号类型 百分比
     *
     * @return
     */
    @PostMapping("/standardizationWarningSignalSignalTypePercentage")
    public Result<Map<String, Object>> standardizationWarningSignalSignalTypePercentage() {
        Map<String, Object> result = standardizationWarningSignalService.signalTypePercentage();
        return Result.ok(result);
    }

    /**
     * 标准化-控制信息 列表、分页
     *
     * @param page
     * @param size
     * @param control_type
     * @param problem_type
     * @return
     */
    @PostMapping("/standardizationControlMsgList")
    public Result standardizationControlMsgList(@RequestParam(required = false, defaultValue = "1") Long page,
                                                @RequestParam(required = false, defaultValue = "10") Long size,
                                                @RequestParam(required = false) String control_type,
                                                @RequestParam(required = false) String problem_type) {
        try {
            Page<StandardizationControlMsg> pageInfo = standardizationControlMsgService.controlMsgPage(control_type, problem_type, page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.ok(new Page<>());
        }
    }

    /**
     * 标准化-控制信息-控制类型
     *
     * @return
     */
    @PostMapping("/standardizationControlMsgControlTypeList")
    public Result standardizationControlMsgControlTypeList() {
        List<String> list = standardizationControlMsgService.findAllControlType();
        return Result.ok(list);
    }

    /**
     * 标准化-控制信息-问题类型列表
     *
     * @return
     */
    @PostMapping("/standardizationControlMsgProblemTypeList")
    public Result standardizationControlMsgProblemTypeList() {
        List<String> list = standardizationControlMsgService.findAllProblemType();
        return Result.ok(list);
    }


    /**
     * 标准化-控制信息-问题类型 百分比
     *
     * @return
     */
    @PostMapping("/standardizationControlMsgProblemTypePercentage")
    public Result<Map<String, Object>> standardizationControlMsgProblemTypePercentage() {
        Map<String, Object> result = standardizationControlMsgService.problemTypePercentage();
        return Result.ok(result);
    }

    /**
     * 标准化-控制信息-控制类型 百分比
     *
     * @return
     */
    @PostMapping("/standardizationControlMsgControlTypePercentage")
    public Result<Map<String, Object>> standardizationControlMsgControlTypePercentage() {
        Map<String, Object> result = standardizationControlMsgService.controlTypePercentage();
        return Result.ok(result);
    }

    /**
     * 标准化-运行数据 列表、分页
     *
     * @param page
     * @param size
     * @param data_type
     * @param device
     * @param problem_type
     * @return
     */
    @PostMapping("/standardizationOperDataList")
    public Result standardizationOperDataList(@RequestParam(required = false, defaultValue = "1") Long page,
                                              @RequestParam(required = false, defaultValue = "10") Long size,
                                              @RequestParam(required = false) String data_type,
                                              @RequestParam(required = false) String device,
                                              @RequestParam(required = false) String problem_type) {
        try {
            Page<StandardizationOperData> pageInfo = standardizationOperDataService.operDataPage(data_type, device, problem_type, page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.ok(new Page<>());
        }
    }

    /**
     * 标准化-运行数据-设备列表
     *
     * @return
     */
    @PostMapping("/standardizationOperDataDeviceList")
    public Result standardizationOperDataDeviceList() {
        List<String> list = standardizationOperDataService.findAllDeviceType();
        return Result.ok(list);
    }

    /**
     * 标准化-运行数据-数据类型列表
     *
     * @return
     */
    @PostMapping("/standardizationOperDataDataTypeList")
    public Result standardizationOperDataDataTypeList() {
        List<String> list = standardizationOperDataService.findAllDataType();
        return Result.ok(list);
    }

    /**
     * 标准化-运行数据-问题类型列表
     *
     * @return
     */
    @PostMapping("/standardizationOperDataProblemTypeList")
    public Result standardizationOperDataProblemTypeList() {
        List<String> list = standardizationOperDataService.findAllProblemType();
        return Result.ok(list);
    }

    /**
     * 标准化-运行数据-设备分类 百分比
     *
     * @return
     */
    @PostMapping("standardizationOperDataDevicePercentage")
    public Result<Map<String, Object>> standardizationOperDataDevicePercentage() {
        Map<String, Object> result = standardizationOperDataService.devicePercentage();
        return Result.ok(result);
    }

    /**
     * 标准化-运行数据-问题类型 百分比
     *
     * @return
     */
    @PostMapping("/standardizationOperDataProblemTypePercentage")
    public Result<Map<String, Object>> standardizationOperDataProblemTypePercentage() {
        Map<String, Object> result = standardizationOperDataService.problemTypePercentage();
        return Result.ok(result);
    }

    /**
     * 运行数据-数据类型 百分比
     *
     * @return
     */
    @PostMapping("/standardizationOperDataDataTypePercentage")
    public Result<Map<String, Object>> standardizationOperDataDataTypePercentage() {
        Map<String, Object> result = standardizationOperDataService.dataTypePercentage();
        return Result.ok(result);
    }

    /**
     * 遗漏检测-点表信息 列表、分页
     *
     * @param page
     * @param size
     * @param station
     * @return
     */
    @PostMapping("/signalOmissionPointTableMsgList")
    public Result SignalOmissionPointTableMsgList(@RequestParam(required = false, defaultValue = "1") Long page,
                                                  @RequestParam(required = false, defaultValue = "10") Long size,
                                                  @RequestParam(required = false) String deviceName,
                                                  @RequestParam(required = false) String deviceType,
                                                  @RequestParam(required = false) String station,
                                                  @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime
                                                          startTime,
                                                  @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime
    ) {
        try {
            Page<SignalmissingData> pageInfo = signalmissingDataService.pageMassage(deviceName, deviceType, station, startTime, endTime, page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 遗漏检测-点表信息-设备类型列表
     *
     * @return
     */
    @PostMapping("/signalOmissionPointTableMsgDeviceTypeList")
    public Result signalOmissionPointTableMsgDeviceTypeList(@RequestParam(required = false) String deviceType) {
        List<String> list = signalOmissionPointTableMsgService.findAllDeviceType(deviceType);
        return Result.ok(list);
    }

    /**
     * 遗漏检测-点表信息-所有厂站列表
     *
     * @return
     */
    @Deprecated
    @PostMapping("/signalOmissionPointTableMsgStationList")
    public Result signalOmissionPointTableMsgStationList() {
        List<String> list = signalOmissionPointTableMsgService.findAllStation();
        return Result.ok(list);
    }

    /**
     * 遗漏检测-点表信息-设备分类 百分比
     *
     * @return
     */
    @PostMapping("/signalOmissionPointTableMsgDeviceTypePercentage")
    public Result<Map<String, Object>> signalOmissionPointTableMsgDeviceTypePercentage(
            @RequestParam(required = false) String station,
            @RequestParam(required = false) String deviceType,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime
                    startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime
    ) {
        Map<String, Object> result = signalmissingDataService.deviceTypePercentage(station, startTime, endTime, deviceType);
        return Result.ok(result);
    }

    /**
     * 遗漏检测-点表信息-厂站 百分比
     *
     * @return
     */
    @PostMapping("/signalOmissionPointTableMsgStationPercentage")
    public Result<Map<String, Object>> signalOmissionPointTableMsgStationPercentage(
            @RequestParam(required = false) String deviceType,
            @RequestParam(required = false) String station,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime
                    startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime
    ) {
        Map<String, Object> result = signalmissingDataService.stationPercentage(deviceType, startTime, endTime, station);
        return Result.ok(result);
    }

    /**
     * 遗漏检测-点表信息-厂站 top5
     *
     * @return
     */
    @PostMapping("/signalOmissionPointTableMsgStationTop5")
    public Result<List<Map<String, Object>>> signalOmissionPointTableMsgStationTop5(
            @RequestParam(required = false) String deviceType,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime
                    startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime
    ) {
        List<Map<String, Object>> result = signalmissingDataService.signalOmissionPointTableMsgStationTop5(deviceType, startTime, endTime);
        return Result.ok(result);
    }

    /**
     * 遗漏检测-实时数据 列表、分页
     *
     * @param page
     * @param size
     * @param data_type
     * @param station
     * @param problem_type
     * @return
     */
    @PostMapping("/signalOmissionRealtimeDataList")
    public Result SignalOmissionRealtimeDataList(@RequestParam(required = false, defaultValue = "1") Long page,
                                                 @RequestParam(required = false, defaultValue = "10") Long size,
                                                 @RequestParam(required = false) String data_type,
                                                 @RequestParam(required = false) String station,
                                                 @RequestParam(required = false) String problem_type) {
        try {
            Page<SignalOmissionRealtimeData> pageInfo = signalOmissionRealtimeDataService.realtimeDataPage(data_type, station, problem_type, page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.ok(new Page<>());
        }
    }

    /**
     * 遗漏检测-实时数据-数据类型列表
     *
     * @return
     */
    @PostMapping("/signalOmissionRealtimeDataDataTypeList")
    public Result signalOmissionRealtimeDataDataTypeList() {
        List<String> list = signalOmissionRealtimeDataService.findAllDataType();
        return Result.ok(list);
    }

    /**
     * 遗漏检测-实时数据-所有厂站列表
     *
     * @return
     */
    @PostMapping("/signalOmissionRealtimeDataStationList")
    public Result signalOmissionRealtimeDataStationList() {
        List<String> list = signalOmissionRealtimeDataService.findAllStation();
        return Result.ok(list);
    }

    /**
     * 遗漏检测-实时数据-问题类型列表
     *
     * @return
     */
    @PostMapping("/signalOmissionRealtimeDataProblemTypeList")
    public Result signalOmissionRealtimeDataProblemTypeList() {
        List<String> list = signalOmissionRealtimeDataService.findAllProblemType();
        return Result.ok(list);
    }

    /**
     * 遗漏检测-实时数据-数据类型 百分比
     *
     * @return
     */
    @PostMapping("/signalOmissionRealtimeDataDataTypePercentage")
    public Result<Map<String, Object>> signalOmissionRealtimeDataDataTypePercentage() {
        Map<String, Object> result = signalOmissionRealtimeDataService.dataTypePercentage();
        return Result.ok(result);
    }

    /**
     * 遗漏检测-实时数据-问题类型 百分比
     *
     * @return
     */
    @PostMapping("/signalOmissionRealtimeDataProblemTypePercentage")
    public Result<Map<String, Object>> signalOmissionRealtimeDataProblemTypePercentage() {
        Map<String, Object> result = signalOmissionRealtimeDataService.problemTypePercentage();
        return Result.ok(result);
    }

    /**
     * 遗漏检测-实时数据-厂站 百分比
     *
     * @return
     */
    @PostMapping("/signalOmissionRealtimeDataStationPercentage")
    public Result<Map<String, Object>> signalOmissionRealtimeDataStationPercentage() {
        Map<String, Object> result = signalOmissionRealtimeDataService.stationPercentage();
        return Result.ok(result);
    }

    /**
     * 遗漏检测-历史数据 列表、分页
     *
     * @param page
     * @param size
     * @param data_type
     * @param station
     * @param problem_type
     * @return
     */
    @PostMapping("/signalOmissionHistoryDataList")
    public Result SignalOmissionHistoryDataList(@RequestParam(required = false, defaultValue = "1") Long page,
                                                @RequestParam(required = false, defaultValue = "10") Long size,
                                                @RequestParam(required = false) String data_type,
                                                @RequestParam(required = false) String station,
                                                @RequestParam(required = false) String problem_type) {
        try {
            Page<SignalOmissionHistoryData> pageInfo = signalOmissionHistoryDataService.historyDataPage(data_type, station, problem_type, page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.ok(new Page<>());
        }
    }

    /**
     * 遗漏检测-历史数据-数据类型列表
     *
     * @return
     */
    @PostMapping("/signalOmissionHistoryDataDataTypeList")
    public Result signalOmissionHistoryDataDataTypeList() {
        List<String> list = signalOmissionHistoryDataService.findAllDataType();
        return Result.ok(list);
    }

    /**
     * 遗漏检测-历史数据-所有厂站列表
     *
     * @return
     */
    @PostMapping("/signalOmissionHistoryDataStationList")
    public Result signalOmissionHistoryDataStationList() {
        List<String> list = signalOmissionHistoryDataService.findAllStation();
        return Result.ok(list);
    }

    /**
     * 遗漏检测-历史数据-问题类型列表
     *
     * @return
     */
    @PostMapping("/signalOmissionHistoryDataProblemTypeList")
    public Result signalOmissionHistoryDataProblemTypeList() {
        List<String> list = signalOmissionHistoryDataService.findAllProblemType();
        return Result.ok(list);
    }

    /**
     * 遗漏检测-历史数据-数据分类 百分比
     *
     * @return
     */
    @PostMapping("/signalOmissionHistoryDataDataTypePercentage")
    public Result<Map<String, Object>> signalOmissionHistoryDataDataTypePercentage() {
        Map<String, Object> result = signalOmissionHistoryDataService.dataTypePercentage();
        return Result.ok(result);
    }

    /**
     * 遗漏检测-历史数据-问题类型 百分比
     *
     * @return
     */
    @PostMapping("/signalOmissionHistoryDataProblemTypePercentage")
    public Result<Map<String, Object>> signalOmissionHistoryDataProblemTypePercentage() {
        Map<String, Object> result = signalOmissionHistoryDataService.problemTypePercentage();
        return Result.ok(result);
    }

    /**
     * 遗漏检测-历史数据-厂站 百分比
     *
     * @return
     */
    @PostMapping("/signalOmissionHistoryDataStationPercentage")
    public Result<Map<String, Object>> signalOmissionHistoryDataStationPercentage() {
        Map<String, Object> result = signalOmissionHistoryDataService.stationPercentage();
        return Result.ok(result);
    }

    /**
     * 频率分析-告警信号 列表、分页 默认查询前一天数据
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/signalFrequencyWarningSignalList")
    public Result signalFrequencyWarningSignalList(@RequestParam(required = false, defaultValue = "1") Long page,
                                                   @RequestParam(required = false, defaultValue = "10") Long size) {
        try {
            Page<SignalfrequencyData> pageInfo = signalfrequencyDataService.page(page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.ok(new Page<>());
        }
    }

    /**
     * 频率分析-告警信号 列表、分页 条件查询（较慢）
     *
     * @param page
     * @param size
     * @param deviceType
     * @param station
     * @param signalType
     * @param actionType
     * @param importantType
     * @param startTime
     * @param endTime
     * @return
     */
    @PostMapping("/conditionSignalFrequencyWarningSignalList")
    public Result conditionSignalFrequencyWarningSignalList(@RequestParam(required = false, defaultValue = "1") Long page,
                                                            @RequestParam(required = false, defaultValue = "10") Long size,
                                                            @RequestParam(required = false) String deviceType,
                                                            @RequestParam(required = false) String station,
                                                            @RequestParam(required = false) String signalType,
                                                            @RequestParam(required = false) String actionType,
                                                            @RequestParam(required = false) String importantType,
                                                            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                                            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime
    ) {
        try {
            Page<SignalfrequencyData> pageInfo = signalfrequencyDataService.conditionPage(deviceType, station, signalType, actionType, importantType, startTime, endTime, page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.ok(new Page<>());
        }
    }

    /**
     * 频率分析-告警信号 列表、分页
     *
     * @param page
     * @param size
     * @param signalType
     * @param importantType
     * @return
     */
    @PostMapping("/signalFrequencyWarningSignalStandardList")
    public Result signalFrequencyWarningSignalStandardList(@RequestParam(required = false, defaultValue = "1") Long
                                                                   page,
                                                           @RequestParam(required = false, defaultValue = "10") Long size,
                                                           @RequestParam(required = false) String signalType,
                                                           @RequestParam(required = false) String solveWay,
                                                           @RequestParam(required = false) String importantType) {
        try {
            Page<SignalfrequencyLevel> pageInfo = signalfrequencyLevelService.page(signalType, importantType, solveWay, page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 频率分析-告警信号-Excel导出
     *
     * @return
     */
    @RequestMapping("/signalFrequencyDataExportExcel")
    public void signalFrequencyDataExportExcel(HttpServletResponse response,
                                               //设备
                                               @RequestParam(required = false) String deviceName,
                                               //厂站
                                               @RequestParam(required = false) String station,
                                               //动作频次
                                               @RequestParam(required = false) String actionType,
                                               //重要程度
                                               @RequestParam(required = false) String importantType,
                                               //信号类别
                                               @RequestParam(required = false) String signalType,
                                               //时间范围
                                               @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime
                                                       startTime,
                                               @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            signalfrequencyDataService.exportExcel(response, deviceName, station, actionType, importantType, signalType, startTime, endTime);
        } catch (Exception e) {
            log.error(e.toString());
        }
    }

    /**
     * 频率分析-告警信号-信号类别
     *
     * @return
     */
    @PostMapping("/signalFrequencyWarningSignalTypeList")
    public Result signalFrequencyWarningSignalTypeList() {
        List<String> list = signalfrequencyDataService.signalFrequencyWarningSignalTypeList();
        return Result.ok(list);
    }

    /**
     * 频率分析-告警信号-动作频次
     *
     * @return
     */
    @PostMapping("/signalFrequencyWarningSignalActionList")
    public Result signalFrequencyWarningSignalActionList() {
        List<String> list = signalfrequencyDataService.signalFrequencyWarningSignalActionList();
        return Result.ok(list);
    }

    /**
     * 频率分析-告警信号-重要程度
     *
     * @return
     */
    @PostMapping("/signalFrequencyWarningSignalImportantList")
    public Result signalFrequencyWarningSignalImportantList() {
        List<String> list = signalfrequencyDataService.signalFrequencyWarningSignalImportantList();
        return Result.ok(list);
    }

    /**
     * 频率分析-告警信号-动作频次
     *
     * @return
     */
    @PostMapping("/signalFrequencyWarningSignalUpdateImportantTypeType")
    public Result signalFrequencyWarningSignalUpdateImportantTypeType(@RequestBody SignalfrequencyData data) {
        signalfrequencyDataService.signalFrequencyWarningSignalUpdateImportantTypeType(data);
        return Result.ok();
    }

    /**
     * 频率分析-历史数据-所有厂站列表
     *
     * @return
     */
    @PostMapping("/signalFrequencyWarningSignalStationList")
    public Result signalFrequencyWarningSignalStationList() {
        List<String> list = signalfrequencyDataService.findAllStation();
        return Result.ok(list);
    }

    /**
     * 频率分析-信号类别占比
     *
     * @return
     */
    @PostMapping("/signalFrequencyWarningSignalTypePercent")
    public Result signalFrequencyWarningSignalTypePercent(@RequestParam(required = false) String deviceType,
                                                          @RequestParam(required = false) String station,
                                                          @RequestParam(required = false) String signalType,
                                                          @RequestParam(required = false) String actionType,
                                                          @RequestParam(required = false) String importantType,
                                                          @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                                          @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        if (StringUtils.isEmpty(deviceType) && StringUtils.isEmpty(station) && StringUtils.isEmpty(signalType) && StringUtils.isEmpty(actionType) && StringUtils.isEmpty(importantType) && null == startTime && null == endTime) {
            // 获取当前时间
            LocalDateTime currentTime = LocalDateTime.now();
            // 当天的凌晨时间
            LocalDateTime startOfToday = LocalDateTime.of(currentTime.toLocalDate(), LocalTime.MIN);
            // 前一天的凌晨时间
            LocalDateTime startOfYesterday = startOfToday.minus(1, ChronoUnit.DAYS);
            // 前一天的最后一秒时间
            LocalDateTime endOfYesterday = startOfYesterday.with(LocalTime.MAX);
            return Result.ok(signalfrequencyDataService.signalFrequencyWarningSignalTypePercent(deviceType, station, signalType, actionType, importantType, startOfYesterday, endOfYesterday));
        }
        return Result.ok(signalfrequencyDataService.signalFrequencyWarningSignalTypePercent(deviceType, station, signalType, actionType, importantType, startTime, endTime));
    }

    /**
     * 频率分析-信号设备占比TOP5
     *
     * @return
     */
    @PostMapping("/signalFrequencyWarningSignalSpeed")
    public Result signalFrequencyWarningSignalSpeed(@RequestParam(required = false) String deviceType,
                                                    @RequestParam(required = false) String station,
                                                    @RequestParam(required = false) String signalType,
                                                    @RequestParam(required = false) String actionType,
                                                    @RequestParam(required = false) String importantType,
                                                    @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                                    @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        if (StringUtils.isEmpty(deviceType) && StringUtils.isEmpty(station) && StringUtils.isEmpty(signalType) && StringUtils.isEmpty(actionType) && StringUtils.isEmpty(importantType) && null == startTime && null == endTime) {
            // 获取当前时间
            LocalDateTime currentTime = LocalDateTime.now();
            // 当天的凌晨时间
            LocalDateTime startOfToday = LocalDateTime.of(currentTime.toLocalDate(), LocalTime.MIN);
            // 前一天的凌晨时间
            LocalDateTime startOfYesterday = startOfToday.minus(1, ChronoUnit.DAYS);
            // 前一天的最后一秒时间
            LocalDateTime endOfYesterday = startOfYesterday.with(LocalTime.MAX);
            return Result.ok(signalfrequencyDataService.signalFrequencyWarningSignalSpeed(deviceType, station, signalType, actionType, importantType, startOfYesterday, endOfYesterday));
        }
        return Result.ok(signalfrequencyDataService.signalFrequencyWarningSignalSpeed(deviceType, station, signalType, actionType, importantType, startTime, endTime));
    }

    /**
     * 频率分析-厂站TOP5
     *
     * @return
     */
    @PostMapping("/signalFrequencyWarningSignalDeviceTop5")
    public Result signalFrequencyWarningSignalDeviceTop5(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        return Result.ok(signalfrequencyDataService.signalFrequencyWarningSignalDeviceTop5(startTime, endTime));
    }

    /**
     * 频率分析-厂站TOP5
     *
     * @return
     */
    @PostMapping("/signalFrequencyWarningSignalStationTop5")
    public Result signalFrequencyWarningSignalStationTop5(@RequestParam(required = false) String deviceType,
                                                          @RequestParam(required = false) String station,
                                                          @RequestParam(required = false) String signalType,
                                                          @RequestParam(required = false) String actionType,
                                                          @RequestParam(required = false) String importantType,
                                                          @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                                          @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        if (StringUtils.isEmpty(deviceType) && StringUtils.isEmpty(station) && StringUtils.isEmpty(signalType) && StringUtils.isEmpty(actionType) && StringUtils.isEmpty(importantType) && null == startTime && null == endTime) {
            // 获取当前时间
            LocalDateTime currentTime = LocalDateTime.now();
            // 当天的凌晨时间
            LocalDateTime startOfToday = LocalDateTime.of(currentTime.toLocalDate(), LocalTime.MIN);
            // 前一天的凌晨时间
            LocalDateTime startOfYesterday = startOfToday.minus(1, ChronoUnit.DAYS);
            // 前一天的最后一秒时间
            LocalDateTime endOfYesterday = startOfYesterday.with(LocalTime.MAX);
            return Result.ok(signalfrequencyDataService.signalFrequencyWarningSignalStationTop5(deviceType, station, signalType, actionType, importantType, startOfYesterday, endOfYesterday));
        }
        return Result.ok(signalfrequencyDataService.signalFrequencyWarningSignalStationTop5(deviceType, station, signalType, actionType, importantType, startTime, endTime));
    }

    @PostMapping(value = "/deviceStandardList", name = "设备名称规范列表")
    public Result deviceStandardList() {
        try {
            List<String> list = standardizationPointService.deviceStandardList();
            return Result.ok(list);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/updateDeviceStandard", name = "编辑设备名称规范列表")
    public Result updateDeviceStandard(@RequestParam(required = false) Integer problemId,
                                       @RequestParam(required = false) String standardName,
                                       @RequestParam(required = false) String standardDesc) {
        try {
            standardizationPointService.updateDeviceStandard(problemId, standardName, standardDesc);
            return Result.ok("保存成功");
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/deviceDescList", name = "规范信息描述")
    public Result deviceDescList(@RequestParam(required = false) String deviceName) {
        try {
            List<String> list = standardizationPointService.deviceDescList(deviceName);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/nameDescList", name = "名称规范设置列表")
    public Result nameDescList(@RequestParam(required = false, defaultValue = "1") Long page,
                               @RequestParam(required = false, defaultValue = "10") Long size,
                               @RequestParam(required = false) String name) {
        try {
            PageResult<StandardizationNameStandard> info = standardizationPointService.nameDescList(page, size, name);
            return Result.ok(info);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/addNameDesc", name = "新增名称规范")
    public Result addNameDesc(@RequestBody(required = false) StandardizationNameStandard NameStandard) {
        try {
            standardizationPointService.addNameDesc(NameStandard);
            return Result.ok("新增成功");
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/updateNameDesc", name = "编辑名称规范")
    public Result updateNameDesc(@RequestBody(required = false) StandardizationNameStandard NameStandard) {
        try {
            standardizationPointService.updateNameDescByIds(NameStandard);
            return Result.ok("修改成功");
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/delNameDesc", name = "删除名称规范")
    public Result DelNameDesc(@RequestParam(required = false) String ids) {
        try {
            standardizationPointService.DelNameDescByIds(ids);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/train", name = "是否训练")
    public Result train(@RequestParam(required = false) Integer problemId) {
        try {
            String result = standardizationPointService.train(problemId);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/dailyTraining", name = "当日训练内容")
    public Result dailyTraining(@RequestParam(required = false, defaultValue = "1") Integer page,
                                @RequestParam(required = false, defaultValue = "10") Integer size,
                                @RequestParam(required = false) String time,
                                @RequestParam(required = false) String pointName) {
        try {
            PageResult<StandardizationPointDTO> info = standardizationPointService.dailyTraining(page, size, time, pointName);
            return Result.ok(info);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 测试信号频率分析
     */
    @PostMapping("/testCountFrequency")
    public void testCountFrequency() {
        LocalDateTime startTime = LocalDateTime.now().minusDays(1);
        LocalDateTime endTime = LocalDateTime.now();
        log.info("开始信号频率分析");
        signalfrequencyDataService.countFrequency(startTime, endTime);
        log.info("信号频率分析完成");
    }

    /**
     * 测试定时分析 点表 更新标准化基础表（厂站，点名会更新）
     *
     * @return
     */
    @PostMapping("/testAnalysisTimingT0007")
    private Result testAnalysisTimingT0007() {
        try {
            standardizationPointService.analysisTimingT0007();
            return Result.ok("点表分析成功");
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 测试模型分析点名，形成一体化信号-标准化基础数据
     *
     * @return
     */
    @PostMapping("/modelAnalysis")
    private Result testModelAnalysis() {
        try {
            standardizationPointService.ModelAnalysisTiming();
            return Result.ok("点表分析成功");
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

}